package com.ltsh.fim.common.impl;

import com.alibaba.fastjson.JSONObject;
import com.ltsh.fim.common.inf.MyEncoder;
import com.ltsh.fim.common.model.FimRequest;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class MyEncoderImpl implements MyEncoder {
    private static Logger logger = LoggerFactory.getLogger(MyEncoderImpl.class);
    @Override
    public ByteBuf encoding(FimRequest request) {
        ByteBuf byteBuf = Unpooled.buffer(16);
        byteBuf.writeByte(request.getType());
        byteBuf.writeLong(request.getId());
        byte[] bytes = JSONObject.toJSONBytes(request.getContext());
        byteBuf.writeInt(bytes.length);
        byteBuf.writeBytes(bytes);
        int writeLength = 1 + 8 + 4 + bytes.length;
        byteBuf.writerIndex(writeLength);
        return byteBuf;
    }
}
